package com.gztms.ordermgnt.easyexcel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @author Zoey
 * @date 2023/5/8 21:26
 * @Description 性别转换类
 **/
public class StatusConverter implements Converter<Integer> {
    private static final String UNPAID = "未支付";
    private static final String PAID = "已支付";
    private static final String CANCEL = "已取消";

    /**
     * 实体类对象属性
     * @return
     */
    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    /**
     * Excel对应的cellData属性
     * @return
     */
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * 读数据
     * @param cellData
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 1、从cell中读取数据
        String status = cellData.getStringValue();
        if (UNPAID.equals(status)) {
            return 0;
        }else if(PAID.equals(status)) {
            return 1;
        }else if(CANCEL.equals(status)) {
            return 2;
        }
        return null;
    }

    /**
     * 写入
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == null) {
            return new WriteCellData<>("");
        }else if (value == 0){
            return new WriteCellData<>(UNPAID);
        }else if(value == 1) {
            return new WriteCellData<>(PAID);
        }else if (value == 2) {
            return new WriteCellData<>(CANCEL);
        }
        return new WriteCellData<>("");
    }
}
